package com.lck.blogmanager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lck.blogmanager.common.Response;
import com.lck.blogmanager.entity.TagEntity;
import com.lck.blogmanager.entity.UserEntity;
import com.lck.blogmanager.req.*;
import com.lck.blogmanager.res.*;
import com.lck.blogmanager.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import static com.lck.blogmanager.Enum.CommonConstant.WINDOWS_FILE_PATH;

/**
 * 用户个人中心
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/4/8 15:23
 */
@RequestMapping("/userCenter")
@RequiredArgsConstructor
@Slf4j
@RestController
@CrossOrigin
public class UserCenterController {

    private final BlogService blogService;
    private final QuestionService questionService;
    private final TagService tagService;
    private final FileService fileService;
    private final UserService userService;

    /**
     * 个人中心博客列表
     * @param req
     * @return
     */
    @PostMapping("/blogPage")
    public Response<Page<PageBlogRes>> pageBlog(@RequestBody PageBlogReq req){
        Page<PageBlogRes> page = blogService.userCenterPageBlog(req);
        return Response.success(page);
    }

    /**
     * 获取博客标签
     * @return
     */
    @GetMapping("/getBlogTag")
    public Response<List<TagEntity>> getBlogTag(){
        List<TagEntity> list = tagService.getBlogTag();
        return Response.success(list);
    }

    /**
     * 添加博客
     * @param req
     * @return
     */
    @PostMapping("/userAddBlog")
    public Response<Integer> addBlog(@RequestBody AddBlogReq req){
        Integer id = blogService.addBlog(req);
        return Response.success(id);
    }

    /**
     * 添加问答
     * @param req
     * @return
     */
    @PostMapping("/userAddQuestion")
    public Response<Integer> addQuestion(@RequestBody AddQuestionReq req){
        Integer id = questionService.addQuestion(req);
        return Response.success(id);
    }


    /**
     * 博客详情
     * @param blogId
     * @return
     */
    @GetMapping("/detail/blog/{blogId}")
    public Response<DetailBlogRes> detailBlog(@PathVariable Integer blogId){
        DetailBlogRes res = blogService.detailBlog(blogId);
        return Response.success(res);
    }


    /**
     * 修改博客
     * @param req
     * @return
     */
    @PostMapping("/userUpdateBlog")
    public Response updateBlog(@RequestBody UpdateBlogReq req){
        blogService.updateBlog(req);
        return Response.success();
    }

    /**
     * 删除博客
     * @param req
     * @return
     */
    @PostMapping("/userDeleteBlog")
    public Response deleteBlog(@RequestBody DeleteBlogReq req){
        blogService.deleteBlog(req);
        return Response.success();
    }


    /**
     * 个人中心问答列表
     * @param req
     * @return
     */
    @PostMapping("/QuestionPage")
    public Response<Page<PageQuestionRes>> pageQuestion(@RequestBody PageQuestionReq req){
        Page<PageQuestionRes> page = questionService.userCenterPageQuestion(req);
        return Response.success(page);
    }


    /**
     * 问答详情
     * @param questionId
     * @return
     */
    @GetMapping("/detail/question/{questionId}")
    public Response<DetailQuestionRes> detailQuestion(@PathVariable Integer questionId){
        DetailQuestionRes res = questionService.detailQuestion(questionId);
        return Response.success(res);
    }

    /**
     * 获取问答标签
     * @return
     */
    @GetMapping("/getQuestionTag")
    public Response<List<TagEntity>> getQuestionTag(){
        List<TagEntity> list = tagService.getQuestionTag();
        return Response.success(list);
    }

    /**
     * 修改问答
     * @param req
     * @return
     */
    @PostMapping("/userUpdateQuestion")
    public Response updateQuestion(@RequestBody UpdateQuestionReq req){
        questionService.updateQuestion(req);
        return Response.success();
    }

    /**
     * 删除问答
     * @param req
     * @return
     */
    @PostMapping("/userDeleteQuestion")
    public Response deleteQuestion(@RequestBody DeleteQuestionReq req){
        questionService.deleteQuestion(req);
        return Response.success();
    }



    /**
     * 用户文件列表
     *
     * @param req
     * @return
     */
    @PostMapping("/filePage")
    public Response<Page<FilePageRes>> pageFile(@RequestBody FilePageReq req) {
        Page<FilePageRes> info = fileService.pageFile(req);
        return Response.success(info);
    }

    /**
     * 用户新增文件
     * @param req
     * @return
     */
    @PostMapping("/userUpdateFile")
    public Response userUpdateFile(@RequestBody UpdateFileReq req){
        fileService.userUpdateFile(req);
        return Response.success();
    }

    /**
     * 用户新增文件
     * @param req
     * @return
     */
    @PostMapping("/userAddFile")
    public Response<Integer> addFile(@RequestBody AddFileReq req){
        Integer id = fileService.addFile(req);
        return Response.success(id);
    }

    /**
     * 上传文件，在新上传文件的时候先调用
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    public Response<String> addFile(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()){
            return Response.serverError("文件上传失败");
        }
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //生成文件名称通用方法
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        Random r = new Random();
        StringBuilder tempName = new StringBuilder();
        tempName.append(sdf.format(new Date())).append(r.nextInt(100)).append(suffixName);
        String newFileName = tempName.toString();
        try {
            //保存文件
            byte[] bytes = file.getBytes();
            Path path = Paths.get(WINDOWS_FILE_PATH+newFileName);
            Files.write(path,bytes);
        }catch (IOException e){
            e.printStackTrace();
        }
        return Response.success(tempName.toString());

    }

    /**
     * 用户删除文件
     * @param req
     * @return
     */
    @PostMapping("/userDeleteFile")
    public Response userDeleteFile(@RequestBody DeleteFileReq req){
        fileService.userDeleteFile(req);
        return Response.success();
    }


    /**
     * 修改密码
     * @param req
     * @return
     */
    @PostMapping("/updatePassword")
    public Response updatePassword(@RequestBody UpdatePasswordReq req){
        userService.updatePassword(req);
        return Response.success();
    }


    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/infoDetail")
    public Response<UserEntity> infoDetail(){
        UserEntity user = userService.infoDetail();
        return Response.success(user);
    }

}
